package com.plian.system.service.yth.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plian.Tools.CollectionUtil;
import com.plian.system.entity.hr.staff.Supervisor;
import com.plian.system.entity.yth.stafffiles.SupervisoryBoard;
import com.plian.system.mapper.yth.staffFiles.SupervisoryBoardMapper;
import com.plian.system.service.yth.ISupervisoryBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class SupervisoryBoardServiceImpl extends ServiceImpl<SupervisoryBoardMapper, SupervisoryBoard> implements ISupervisoryBoardService {
    private final String YTH_DATASOURCE_TYPE = "3";
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
//    @DS("slave_1")
    @Override
    public List<Supervisor> getAllProject(Date startTime, Date endTime) {
        List<Supervisor> supervisors = new ArrayList<>();

        List<SupervisoryBoard> supervisoryBoards = list(new LambdaQueryWrapper<SupervisoryBoard>()
                .gt(Optional.ofNullable(startTime).isPresent(),SupervisoryBoard::getCreateTime,startTime)
                .le(Optional.ofNullable(endTime).isPresent(),SupervisoryBoard::getCreateTime,endTime)
        );
        if (CollectionUtil.isNotEmpty(supervisoryBoards)){
            supervisors = conversion(supervisoryBoards);
        }
        return supervisors;
    }
//    @DS("slave_1")
    private List<Supervisor> conversion(List<SupervisoryBoard> supervisoryBoards){
        List<Supervisor> supervisors= new ArrayList<>();
        ZoneId zone = ZoneId.systemDefault();
        for (SupervisoryBoard supervisoryBoard:supervisoryBoards){
            Supervisor supervisor = new Supervisor();
            Optional.ofNullable(supervisoryBoard.getCreateDept()).ifPresent(m->{
                supervisor.setCompanyId(m.toString());
                    });
            supervisor.setStaffName(supervisoryBoard.getEmployeeFile());
            supervisor.setIsOutSupervisor(Integer.valueOf(supervisoryBoard.getIsEnterprisePersonnel()));
            Optional.ofNullable(supervisoryBoard.getSex()).ifPresent(m->{
                if(m == 1) supervisor.setGenderId("020001");
                else supervisor.setGenderId("020002");
            });
            /*switch (supervisoryBoard.getSex()) {
                case 1:
                    supervisor.setGenderId("020001");
                    break;
                case 2:
                    supervisor.setGenderId("020002");
                    break;
                default:
                    break;
            }*/
            switch (supervisoryBoard.getSupervisoryPosition()){
                case 1:
                    supervisor.setSupervisorPositionId("041001");
                    break;
                case 2:
                    supervisor.setSupervisorPositionId("041002");
                    break;
                case 3:
                    supervisor.setSupervisorPositionId("041003");
                    break;
                case 4:
                    supervisor.setSupervisorPositionId("041004");
                    break;
                case 5:
                    supervisor.setSupervisorPositionId("041005");
                    break;
                default:
                    break;
            }
            Optional.ofNullable(supervisoryBoard.getBirthday()).ifPresent( m ->{
                supervisor.setBirthday(Date.from(m.atStartOfDay().atZone(zone).toInstant()));
            });
            Optional.ofNullable(supervisoryBoard.getWorkingDate()).ifPresent( m ->{
                supervisor.setAppointmentTime(String.valueOf(Date.from(m.atStartOfDay().atZone(zone).toInstant())));
            });
            supervisor.setCreateCompanyId(supervisoryBoard.getCreateDept().toString());
            supervisor.setCreateUser(supervisoryBoard.getCreateUser().toString());
            supervisor.setUpdateUser(supervisoryBoard.getUpdateUser().toString());
            supervisor.setCreateTime(supervisoryBoard.getCreateTime());
            supervisor.setUpdateTime(supervisoryBoard.getUpdateTime());

            supervisor.setDataSource(YTH_DATASOURCE_TYPE);
            supervisor.setDataSourceId(supervisoryBoard.getId().toString());
            supervisors.add(supervisor);

        }
        return supervisors;
    }
}
